﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Catering.Catering_Page.system
{
    public partial class Moon : System.Web.UI.Page
    {
        static string _classid = "";        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                _classid = Request.QueryString["i"];
                string classname = Request.QueryString["n"];
                this.txt_classId.Value = _classid;
                this.txt_CName.Text = classname;

                this.LoadRep(_classid, classname);
            }
        }

        /// <summary>
        /// 加载Rep
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="classname"></param>
        public void LoadRep(string classid, string classname)
        {
            if (!string.IsNullOrEmpty(classid) && !string.IsNullOrEmpty(classname))
            {
                try
                {
                    Catering_Model.ClassInfo classinfo = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(classid));
                    DataTable dt_Moon = new Catering_Rules.Moon().GetList(" ClassId = " + classid).Tables[0];
                    dt_Moon.Columns.Add("tempId");
                    dt_Moon.Columns.Add("ClassName");

                    for (int i = 0; i < dt_Moon.Rows.Count; i++)
                    {
                        dt_Moon.Rows[i]["tempId"] = i + 1;
                        dt_Moon.Rows[i]["ClassName"] = classinfo.CName;
                    }
                    this.Rep_Moon.DataSource = dt_Moon;
                    this.Rep_Moon.DataBind();
                }
                catch
                {

                }
            }
        }

        //添加
        [WebMethod]
        public static string Add(string moonName, /*string classId,*/ string Back)
        {
            Catering_Model.Moon _moon = new Catering_Model.Moon();
            _moon.ClassId = /*Convert.ToInt32(classId)*/int.Parse(_classid);
            _moon.MoonName = moonName;
            _moon.Remark = Back;
            new Catering_Rules.Moon().Add(_moon);
            return "OK";
        }

        //修改
        [WebMethod]
        public static string Edit(string _id, string moonName, string classId, string Back)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(_id, out id);
            if (id <= 0) return "NO";

            Catering_Model.Moon _moon = new Catering_Model.Moon();

            _moon.Id = id;
            _moon.MoonName = moonName;
            _moon.ClassId = Convert.ToInt32(classId);
            _moon.Remark = Back;
            new Catering_Rules.Moon().Update(_moon);

            return "OK";
        }

        [WebMethod]
        public static ArrayList Get(string _id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.Moon md = new Catering_Rules.Moon().GetModel(Convert.ToInt32(_id));
            al.Add(md.ClassId);
            al.Add(md.Id);
            al.Add(md.MoonName);
            al.Add(md.Remark);
            return al;
        }

        protected void Rep_Moon_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.Moon().Delete(delID))
                {
                    this.LoadRep(txt_classId.Value, txt_CName.Text);
                }
            }
        }
    }
}